package fr.babostesting.bugs
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TouchEvent;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import fr.zapiks.popup.fonts.HelveticaNeueBold;
	import fr.zapiks.popup.fonts.PopulaireLight;
	import fr.zapiks.popup.fonts.PopulaireRegular;
	import net.hires.debug.Stats;
	
	/**
	 * @author ZoulouX
	 */
	public class IOSTypoBug extends Sprite
	{
		[Embed(source="../../../../lib/graphics/earth.jpg")]
		public static const EarthImage:Class;
		public static const __earthImageData:BitmapData = (new EarthImage() as Bitmap).bitmapData;
		
		
		protected static var LOREM:String = 'Lorem ipsum dolor sit <a href="">amet</a>, consectetur adipiscing elit. Pellentesque feugiat fringilla augue, eu gravida libero lobortis sit amet. Donec non adipiscing lectus. Vestibulum porta semper neque. Cras placerat ullamcorper ligula non dictum. Phasellus congue erat vitae nulla aliquam imperdiet. Etiam pharetra scelerisque sagittis. Integer sit amet hendrerit quam. In porta faucibus ipsum, ut consequat eros dignissim faucibus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc vel nunc erat. Praesent leo nibh, eleifend at sodales et, hendrerit et massa. Proin placerat, lacus at fringilla porta, nisl mauris dictum dolor, ut sollicitudin eros metus at purus. Proin ullamcorper, lectus sit amet placerat viverra, elit purus ornare tortor, sit amet aliquet metus sapien ut mi. Integer ac imperdiet sapien. Donec eget nulla diam. Cras sit amet tellus nulla, eu feugiat libero. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque feugiat fringilla augue, eu gravida libero lobortis sit amet. Donec non adipiscing lectus. Vestibulum porta semper neque. Cras placerat ullamcorper ligula non dictum. Phasellus congue erat vitae nulla aliquam imperdiet. Etiam pharetra scelerisque sagittis. Integer sit amet hendrerit quam. In porta faucibus ipsum, ut consequat eros dignissim faucibus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc vel nunc erat. Praesent leo nibh, eleifend at sodales et, hendrerit et massa. Proin placerat, lacus at fringilla porta, nisl mauris dictum dolor, ut sollicitudin eros metus at purus. Proin ullamcorper, lectus sit amet placerat viverra, elit purus ornare tortor, sit amet aliquet metus sapien ut mi. Integer ac imperdiet sapien. Donec eget nulla diam. Cras sit amet tellus nulla, eu feugiat libero. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque feugiat fringilla augue, eu gravida libero lobortis sit amet. Donec non adipiscing lectus. Vestibulum porta semper neque. Cras placerat ullamcorper ligula non dictum. Phasellus congue erat vitae nulla aliquam imperdiet. Etiam pharetra scelerisque sagittis. Integer sit amet hendrerit quam. In porta faucibus ipsum, ut consequat eros dignissim faucibus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc vel nunc erat. Praesent leo nibh, eleifend at sodales et, hendrerit et massa. Proin placerat, lacus at fringilla porta, nisl mauris dictum dolor, ut sollicitudin eros metus at purus. Proin ullamcorper, lectus sit amet placerat viverra, elit purus ornare tortor, sit amet aliquet metus sapien ut mi. Integer ac imperdiet sapien. Donec eget nulla diam. Cras sit amet tellus nulla, eu feugiat libero.';
		
		protected var _DPIScale:Number;
		protected var _textField:TextField;
		protected var _bitmapData:BitmapData;
		protected var _container:Sprite;
		
		public function IOSTypoBug ()
		{
			if (stage != null)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		protected function init (event:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			_bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true);
			var _bitmap:Bitmap = new Bitmap(_bitmapData);
			
			addChild(_bitmap);
			
			_bitmap.scaleX = .5;
			_bitmap.scaleY = .5;
			
			addChild(new Bitmap(__earthImageData));
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			stage.quality = StageQuality.LOW;
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			
			stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchEventHandler);
			stage.addEventListener(TouchEvent.TOUCH_MOVE, touchEventHandler);
			stage.addEventListener(TouchEvent.TOUCH_END, touchEventHandler);
			
			
			
			
			_container = new Sprite();
			addChild(_container);
			
			_DPIScale = Capabilities.screenDPI / 163;
			
			_container.scaleX = _DPIScale;
			_container.scaleY = _DPIScale;
			
			
			_container.addChild(new Stats());
			
			_container.alpha = .8
			
			var sprite:Sprite;
			var oldSprite:Sprite = _container;
			for (var i:int = 0; i < 15; i++) 
			{
				sprite = new Sprite();
				
				sprite.alpha = .8 + Math.random() / 2;
				sprite.x = .8 + Math.random() / 2;
				sprite.y = .8 + Math.random() / 2;
				
				oldSprite.addChild(sprite);
				
				oldSprite = sprite;
			}
			
			_textField = new TextField();
			
			_textField.mouseEnabled = false;
			
			_textField.multiline = true;
			_textField.wordWrap = true;
			
			_textField.defaultTextFormat = new TextFormat(
				(new PopulaireRegular().fontName),
				40,
				0xFFFFFF
			);
			
			//_textField.antiAliasType = AntiAliasType.ADVANCED;
			_textField.antiAliasType = AntiAliasType.NORMAL;
			
			_textField.embedFonts = true;
			
			_textField.text = LOREM;
			
			_textField.x = 10;
			_textField.y = 10;
			
			_textField.border = true;
			_textField.borderColor = 0xFF0000;
			
			_textField.width = stage.stageWidth / _DPIScale- 20;
			_textField.height = stage.stageHeight / _DPIScale / 2 - 20;
			
			//_textField.cacheAsBitmap = true;
			
			sprite.addChild(_textField);
			
			
			_textField = new TextField();
			
			_textField.mouseEnabled = false;
			
			_textField.multiline = true;
			_textField.wordWrap = true;
			
			_textField.defaultTextFormat = new TextFormat(
				(new HelveticaNeueBold().fontName),
				12,
				0xFFFFFF
			);
			
			_textField.antiAliasType = AntiAliasType.ADVANCED;
			//_textField.antiAliasType = AntiAliasType.NORMAL;
			
			_textField.embedFonts = true;
			
			_textField.htmlText = LOREM;
			
			_textField.x = 10;
			_textField.y = stage.stageHeight / 2 + 20;
			
			_textField.border = true;
			_textField.borderColor = 0xFF0000;
			
			_textField.width = stage.stageWidth / _DPIScale- 20;
			_textField.height = stage.stageHeight / _DPIScale / 2 - 20;
			
			//_textField.cacheAsBitmap = true;
			
			sprite.addChild(_textField);
			
		}
		
		protected function enterFrameHandler (event:Event):void
		{
			_container.y  = _container.y;
			return;
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
			
			_bitmapData.fillRect(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height), 0xFF0000);
			_bitmapData.draw(stage);
		}
		
		protected function touchEventHandler (event:TouchEvent):void
		{
			if (event.isPrimaryTouchPoint)
			{
				//_textField.width = Math.max(2, event.stageX / _DPIScale - 20);
				//_textField.height = Math.max(2, event.stageY / _DPIScale - 20);
				
				_container.x = event.stageX / _DPIScale - stage.stageWidth / 2;
				_container.y = event.stageY / _DPIScale - stage.stageWidth / 2;
			}
		}
	}
}